package com.rerun.common.sms;

import org.apache.commons.lang3.RandomUtils;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.ResourceBundle;


/**
 * The type Sms util.
 */
public class SmsUtil {
	private static ResourceBundle resb1 = ResourceBundle.getBundle("properties/smsYtx");
	/**
	 * 获取随机数
	 */
	private static final String[] NUMS = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
	private static String BASEURL = resb1.getString("BASEURL");//生产环境
	private static String accountSid = resb1.getString("accountSid");
	private static String accountToken = resb1.getString("accountToken");
	private static String AppId = resb1.getString("AppId");
	private static String PORT = resb1.getString("PORT");

	/**
	 * Send sms string.
	 *
	 * @param phone the phone
	 * @param data  数组格式里面有验证码参数和时长参数
	 * @param id    the id
	 * @return string
	 */
	private static String sendSms(String phone, String[] data, String id){

		CCPRestSmsSDK restAPI  = new CCPRestSmsSDK();
		restAPI.init(BASEURL, PORT);
		restAPI.setAccount(accountSid, accountToken);
		restAPI.setAppId(AppId);
		HashMap<String, Object> result= restAPI.sendTemplateSMS(phone,id,data);
		String r_d = (String) result.get("statusCode");
		if(!r_d.equals("000000"))Logger.getLogger(SmsUtil.class).error(result.toString());
		return r_d;
	}

	/**
	 * 发短信
	 *
	 * @param template the template
	 * @return string
	 */
	public static String sendSms(SmsTemplate template){
		return sendSms(template.getPhone(), template.getData(), template.getId());
	}
	


	/**
	 * Get random num string.
	 *
	 * @param num the num
	 * @return the string
	 */
	public static String getRandomNum(int num){
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < num; i++) {
			int n = RandomUtils.nextInt(0, 9);
			if(i == 0 && n == 0){
				n = RandomUtils.nextInt(1,9);
			}
			sb.append(NUMS[n]);
		}
		return sb.toString();
	}
	
}
